package org.itboys.advertisement.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.advertisement.entity.Adzone;
import org.itboys.advertisement.service.AdzoneService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 广告位
 * @author weisky
 *
 */
@RestController 
@RequestMapping("/adzone") 
public class AdzoneController extends BaseController{

	@Autowired
	private AdzoneService adzoneService;
	
	/**
	 * 跳到广告位管理页面 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toListVM")
	public ModelAndView toListVM(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("/advertisement/adzone");
	} 
	
	@RequestMapping("/list")
	public void list(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		Page<Adzone> page = adzoneService.pageQuery(param, request);
		JsonPageUtils.renderJsonPage(page.getTotalPages(), page.getData(), response);
	}
	
	/**
	 * 新增或者修改
	 * @param adzone
	 * @param request
	 * @param response
	 */
	@RequestMapping("/save")
	public void save(Adzone adzone, 
			HttpServletRequest request, HttpServletResponse response){
		int result = adzoneService.insert(adzone);
		AjaxUtils.renderJson(response, result);
	}
	
	/**
	 * 获取一条数据
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getById")
	public void getById(Long id, HttpServletRequest request, HttpServletResponse response){
		AjaxUtils.renderJson(response, adzoneService.getById(id));
	}
	
	/**
	 * 删除一条数据
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delete")
	public void delete(Long id, HttpServletRequest request, HttpServletResponse response){
		boolean result = adzoneService.dele(id);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
}
